package com.ruoyi.chat.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.chat.domain.ChatGroup;
import com.ruoyi.chat.service.IChatGroupService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 群Controller
 * 

 * @date 2023-07-23
 */
@RestController
@RequestMapping("/group")
public class ChatGroupController extends BaseController
{
    @Autowired
    private IChatGroupService chatGroupService;

    /**
     * 查询群列表
     */
    @RequiresPermissions("chat:group:list")
    @GetMapping("/list")
    public TableDataInfo list(ChatGroup chatGroup)
    {
        startPage();
        List<ChatGroup> list = chatGroupService.selectChatGroupList(chatGroup);
        return getDataTable(list);
    }

    /**
     * 导出群列表
     */
    @RequiresPermissions("chat:group:export")
    @Log(title = "群", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatGroup chatGroup)
    {
        List<ChatGroup> list = chatGroupService.selectChatGroupList(chatGroup);
        ExcelUtil<ChatGroup> util = new ExcelUtil<ChatGroup>(ChatGroup.class);
        util.exportExcel(response, list, "群数据");
    }

    /**
     * 获取群详细信息
     */
    @RequiresPermissions("chat:group:query")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId)
    {
        return success(chatGroupService.selectChatGroupByGroupId(groupId));
    }

    /**
     * 根據用戶id查询群消息
     */
    @GetMapping(value = "/AllGroupLastOne")
    public AjaxResult getAllGroupLastOne(Long userId)
    {
        return success(chatGroupService.AllGroupLastOne(userId));
    }

    /**
     * 根據群id查询群消息
     *//*
    @GetMapping(value = "/GroupMessageLastOne")
    public AjaxResult getAllGroupMessageLastOne(Long groupId)
    {
        return success(chatGroupService.AllGroupMessageLastOne(groupId,1));
    }*/


    /**
     * 获取群成员信息
     */
    @RequiresPermissions("chat:group:query")
    @GetMapping(value = "/list/{groupId}")
    public AjaxResult getGroupUser(@PathVariable("groupId") Long groupId)
    {
        return success(chatGroupService.selectChatGroupUserById(groupId));
    }

    /**
     * 获取群聊成员
     */

    @GetMapping(value = "/GroupUser")
    public AjaxResult getGroupUsers(Long groupId)
    {
        return success(chatGroupService.selectChatGroupUserByGroupId(groupId));
    }

    /**
     * 新增群
     */
    @RequiresPermissions("chat:group:add")
    @Log(title = "群", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatGroup chatGroup)
    {
        return toAjax(chatGroupService.insertChatGroup(chatGroup));
    }

    /**
     * 修改群
     */
    @RequiresPermissions("chat:group:edit")
    @Log(title = "群", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatGroup chatGroup)
    {
        return toAjax(chatGroupService.updateChatGroup(chatGroup));
    }

    /**
     * 删除群
     */
    @RequiresPermissions("chat:group:remove")
    @Log(title = "群", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds)
    {
        return toAjax(chatGroupService.deleteChatGroupByGroupIds(groupIds));
    }

    /**
     * 获取商家信息
     */
//    @RequiresPermissions("chat:group:query")
//    @GetMapping(value = "/{businessId}")
//    public AjaxResult businessId(@PathVariable("businessId") Long businessId)
//    {
//        return success(chatGroupService.selectChatGroupByBusinessId(businessId));
//    }
}
